#!/usr/bin/perl

use Getopt::Long;
use Term::ANSIColor;
#use Smart::Comments;

my $pass_cfg_file_name;
my $fail_cfg_file_name;
my $ignore_cfg_file_name;
my $report_dir_name;
my $regression_dir;
my $test_name;
my $seed_index;
my $error_log_on;
my $log_file_full_path_name;

$regression_dir = "build";
GetOptions(
    'pass_cfg_file=s'   => \$pass_cfg_file_name,
    'fail_cfg_file=s'   => \$fail_cfg_file_name,
    'ignore_cfg_file=s' => \$ignore_cfg_file_name,
    'f=s'               => \$log_file_full_path_name,
    'report_dir=s'      => \$report_dir_name,
    'reg_dir=s'         => \$regression_dir,
    'log!'              => \$error_log_on,
);

my @pass_pattern_list  =(
   'simulation pass',
);

my @fail_pattern_list =(
    'error',
    '\*E',
    'fatal',
    'failed',
    'stack trace follows',
    'core dumped',
    'quit',
    'offending'
);

my @ignore_pattern_list =();

if($pass_cfg_file_name  ne undef) {
  my @tmp_valid_array = &get_valid_array_from_file($pass_cfg_file_name);
  if(scalar(@tmp_valid_array) != 0) {
    push @pass_pattern_list, @tmp_valid_array;
  }
}

if($fail_cfg_file_name   ne undef) {
  my @tmp_valid_array = &get_valid_array_from_file($fail_cfg_file_name);
  if(scalar(@tmp_valid_array) != 0) {
    push @fail_pattern_list, @tmp_valid_array;
  }
}

if($ignore_cfg_file_name ne undef) {
  my @tmp_valid_array = &get_valid_array_from_file($ignore_cfg_file_name);
  if(scalar(@tmp_valid_array) != 0) {
    push @ignore_pattern_list, @tmp_valid_array;
  }
  my @tmp_valid_array = @pass_pattern_list;
  if(scalar(@tmp_valid_array) != 0) {
    push @ignore_pattern_list, @tmp_valid_array;
  }
}


my $error_cnt=0;
if($log_file_full_path_name eq "") {
  print "ERROR: scan file is not defined, please use -f to indicate your log file, which want to be analysis\n";
  exit(1)
}
$error_cnt = &log_file_analysis($log_file_full_path_name);


if($error_cnt != 0) {
  #print color('blink bold green on_black');
  #print (" PASS!!!");
  #if($report_file_name ne undef) {
  #  print REPORT_FILE (" PASS!!!");
  #}
  exit(1);
} else {
  exit(0);
}
#-----------------------------------------------
#            SUB MODULE BLOCK
#-----------------------------------------------

sub get_valid_array_from_file {
    my $file_name = shift @_;
    my @valid_value_list=();
    open IN_FILE, "$file_name" or warn " Can't open $file_name:$!";
    #print "  $file_name\n";
    while(<IN_FILE>) {
        my $curr_line = $_;
        if($curr_line =~ /^\s*(\/\/)/ || $curr_line =~ /^\s*$/) {
            next;
        }
        #if($curr_line =~ /^\s*(\w.*)(\/\/.*)?$/i) {
        if($curr_line =~ /^\s*(\S.*)(\/\/.*)?$/i) {
            my $valid_value = $1;
            $valid_value =~ s/([\[|\\|\'|\"|\]|\|\/)|\(|\)])/\\$1/g;
            $valid_value =~ s/\\(\\[d|w|s|b])/$1/ig;
            #print " Debug : $valid_value\n";
            push @valid_value_list, $valid_value;
            #print "  $file_name -> ".$valid_value."\n";
        }
    }
    return @valid_value_list;
}

sub log_file_analysis {
  my $log_file_name = shift @_;
  my $m_error_cnt = 0;
  my $m_pass_cnt  = 0;
  my @ignore_line_list;
  if(!open LOG_FILE, "$log_file_name" ) {
    print "Can't open $log_file_name:$!\n";
    exit(1)
  }
  while(<LOG_FILE>) {
      chomp();
      my $current_line = $_;
      foreach my $fail_pattern (@fail_pattern_list) {
      my $ignore_flag;
      $ignore_flag = 0;
        if($current_line =~ /$fail_pattern/i) {
        foreach my $ignore_pattern (@ignore_pattern_list) {
          if( $current_line =~ /$ignore_pattern/i) {
            push @ignore_line_list, $current_line;
            $ignore_flag = 1;
            print color('bold green');
          }
        }
        if($ignore_flag == 0) {
          print color('bold red');
          $m_error_cnt ++;
        }
        if($error_log_on == 1) {
          print " $current_line\n";
          print color('reset');
          print color('clear');
          last;
        }
      }
    }
    foreach my $pass_pattern (@pass_pattern_list) {
      if($current_line =~ /$pass_pattern/i) {
          $m_pass_cnt++;
      }
    }
  }
  if($m_pass_cnt == 0) {
      $m_error_cnt++;
      print ("  No PASS PATTERN is Caught!!!\n");
  }
  print color('reset');
  print color('clear');
  my $fh;
  if(exists $ENV{"STAR_SCAN_IGNORE_FILE"}) {
    my $ignore_file_name;
    if($log_file_full_path_name =~ /^\s*(\S+)\/\S+(\s*$|\s+)/) {
      $ignore_file_name = $1."\/scan_ignore_line.log";
    }
    print "ignore_file_name = $ignore_file_name\n";
    if(!open ($fh, '>', $ignore_file_name)) {
      print "Can't open $ignore_file_name:$!"; 
      exit(1);
    } else {
      print $fh ("|--------------------------------------------------------------------------------\n");
      print $fh ("| SCAN   LOG FILE: $log_file_full_path_name\n");
      if($pass_cfg_file_name   ne undef) { print $fh ("| PASS   CFG FILE: $pass_cfg_file_name\n") };
      if($fail_cfg_file_name   ne undef) { print $fh ("| FAIL   CFG FILE: $log_file_full_path_name\n") };
      if($ignore_cfg_file_name ne undef) { print $fh ("| IGNORE CFG FILE: $log_file_full_path_name\n") };
      print $fh ("|--------------------------------------------------------------------------------\n");
      foreach my $cur_line (@ignore_line_list) {
        print $fh ("$cur_line\n");
      }
    }
  }
  return $m_error_cnt;
}

